package com.cw.jeeyt.service.impl.security;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.common.utils.StringUtils;
import com.cw.jeeyt.service.bo.security.SecurityResourceBo;
import com.cw.jeeyt.service.bo.security.SecurityRoleBo;
import com.cw.jeeyt.service.bo.security.SecurityRoleVo;
import com.cw.jeeyt.dao.domain.security.SecurityRoleModel;
import com.cw.jeeyt.dao.intf.security.SecurityRoleDao;
import com.cw.jeeyt.service.converter.security.SecurityRoleConverter;
import com.cw.jeeyt.service.intf.security.SecurityResourceService;
import com.cw.jeeyt.service.intf.security.SecurityRoleService;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * 角色信息业务实现类
 *
 * @author chenw
 * @version 1.0.0
 */
@Service
public class SecurityRoleServiceImpl implements SecurityRoleService {

    private static final Logger logger = LoggerFactory.getLogger(SecurityRoleServiceImpl.class);
    @Autowired
    private SecurityRoleDao securityRoleDao;
    @Autowired
    private SecurityResourceService securityResourceService;

    /**
     * 分页查询角色信息
     *
     * @param roleBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<SecurityRoleBo> queryByPage(SecurityRoleBo roleBo, Integer startNum, Integer pageSize)
    throws Exception {

        if (roleBo == null) {
            logger.error("角色信息为空");
            return null;
        }
        PageInfo<SecurityRoleModel> pageInfo = securityRoleDao.queryByPage(SecurityRoleConverter.bo2Model(roleBo),
            startNum, pageSize);
        return new PageInfo<>(pageInfo, SecurityRoleConverter.modelList2BoList(pageInfo.getList()));
    }

    /**
     * 根据主键查寻角色信息
     *
     * @param id
     * @return
     */
    @Override
    public SecurityRoleBo getRoleById(Long id) throws Exception {

        return SecurityRoleConverter.do2Bo(securityRoleDao.getById(id));
    }

    /**
     * 新增角色信息
     *
     * @param roleBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insert(SecurityRoleBo roleBo) throws Exception {

        if (roleBo == null) {
            logger.error("角色信息为空");
            return null;
        }
        return securityRoleDao.insert(SecurityRoleConverter.bo2Model(roleBo));
    }

    /**
     * 更新角色信息
     *
     * @param roleBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(SecurityRoleBo roleBo) throws Exception {

        if (roleBo == null) {
            logger.error("角色信息为空");
            return;
        }
        securityRoleDao.update(SecurityRoleConverter.bo2Model(roleBo));
    }

    /**
     * 删除角色信息
     *
     * @param roleBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(SecurityRoleBo roleBo) throws Exception {

        if (roleBo == null) {
            logger.error("角色信息为空");
            return;
        }
        securityRoleDao.delete(SecurityRoleConverter.bo2Model(roleBo));
    }

    @Override
    public List<SecurityRoleBo> getRolesByResId(Long resId) throws Exception {
        return SecurityRoleConverter.modelList2BoList(securityRoleDao.getRolesByResId(resId));
    }

    @Override
    public List<SecurityRoleBo> getRolesByUserName(String username) throws Exception {

        if (StringUtils.isEmpty(username)) {
            logger.error("用户名称为空");
            return null;
        }
        return SecurityRoleConverter.modelList2BoList(securityRoleDao.getRolesByUserName(username));
    }

    @Override
    public List<SecurityRoleVo> getRolesByUserId(Long userId) throws Exception {
        if (userId == null) {
            logger.error("用户Id为空");
            return null;
        }
        List<SecurityRoleModel> list_role = securityRoleDao.getRolesByUserId(userId);
        List<SecurityRoleVo> roleVos = SecurityRoleConverter.modelList2VoList(list_role);
        if (roleVos != null && !roleVos.isEmpty()) {
            for (SecurityRoleVo securityRoleVo : roleVos) {
                Long roleId = securityRoleVo.getId();
                List<SecurityResourceBo> securityResourceList = securityResourceService.getResourceByRoleId(roleId);
                securityRoleVo.setResources(securityResourceList);
            }
        }
        return roleVos;
    }

    /**
     * 根据角色名称加载角色信息
     */
    @Override
    public SecurityRoleBo getRoleByRoleName(String roleName) {
        return SecurityRoleConverter.model2Bo(securityRoleDao.getRoleByRoleName(roleName));
    }

    /**
     * 根据URL查询角色资源信息
     *
     * @param url
     * @return
     * @throws Exception
     */
    @Override
    public List<SecurityRoleVo> getRolesInfoByUrl(String url) throws Exception {
        List<SecurityRoleModel> roleList = securityRoleDao.getRolesByUrl(url);
        List<SecurityRoleVo> roleVos = SecurityRoleConverter.modelList2VoList(roleList);
        if (roleVos != null && !roleVos.isEmpty()) {
            for (SecurityRoleVo securityRole : roleVos) {
                List<SecurityResourceBo> resourceList = securityResourceService.getResourceByRoleId(
                    securityRole.getId());
                securityRole.setResources(resourceList);
            }
        }
        return roleVos;
    }

    /**
     * 根据条件查询角色列表信息
     *
     * @throws Exception
     */
    @Override
    public List<SecurityRoleVo> queryListByCondition(SecurityRoleBo securityRoleBo) throws Exception {
        List<SecurityRoleModel> roleList = securityRoleDao.queryListByCondition(
            SecurityRoleConverter.bo2Model(securityRoleBo));
        List<SecurityRoleVo> roleVos = SecurityRoleConverter.modelList2VoList(roleList);
        if (roleVos != null && !roleVos.isEmpty()) {
            for (SecurityRoleVo securityRole : roleVos) {
                List<SecurityResourceBo> resourceList = securityResourceService.getResourceByRoleId(
                    securityRole.getId());
                securityRole.setResources(resourceList);
            }
        }
        return roleVos;
    }
}
